﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SchemaRepositoryDsTable.cs" company="Data Ductus Malmö">
//   (c) 2010 - 2012 Data Ductus Malmö AB
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace DataDuctus.Snmp.Persistor.Core.Schema
{
  using System;
  using System.Collections.Generic;
  using System.Linq;

  using Lextm.SharpSnmpLib;

  /// <summary>
  /// The schema repository ds table.
  /// </summary>
  public class SchemaRepositoryDsTable : ICloneable
  {
    #region Static Fields

    /// <summary>
    /// The empty table.
    /// </summary>
    internal static readonly SchemaRepositoryDsTable EmptyTable = new SchemaRepositoryDsTable();

    #endregion

    #region Fields

    /// <summary>
    /// The my columns.
    /// </summary>
    protected IList<ISchemaItem> MyColumns = new List<ISchemaItem>();

    #endregion

    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="SchemaRepositoryDsTable"/> class.
    /// </summary>
    /// <param name="fqName">
    /// The fq name.
    /// </param>
    /// <param name="item">
    /// The item.
    /// </param>
    internal SchemaRepositoryDsTable(string fqName, ISchemaItem item)
    {
      int idx = fqName.LastIndexOf('.');

      this.Namespace = fqName.Substring(0, idx);
      this.Name = fqName.Substring(idx + 1);

      if (item.Type == DefinitionType.Table)
      {
        foreach (ISchemaItem col in item.Children.First().Children)
        {
          this.MyColumns.Add(col);
        }
      }

      if (item.Type == DefinitionType.OidValueAssignment)
      {
        foreach (ISchemaItem col in item.Children)
        {
          if (col.Type == DefinitionType.Scalar)
          {
            this.MyColumns.Add(col);
          }
        }
      }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="SchemaRepositoryDsTable"/> class.
    /// </summary>
    protected SchemaRepositoryDsTable()
    {
      this.Name = "Empty";
      this.Namespace = "Empty";
    }

    #endregion

    #region Public Properties

    /// <summary>
    /// Gets the columns.
    /// </summary>
    public virtual IList<ISchemaItem> Columns
    {
      get
      {
        return this.MyColumns;
      }
    }

    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    public string Name { get; protected set; }

    /// <summary>
    /// Gets or sets the namespace.
    /// </summary>
    public string Namespace { get; protected set; }

    #endregion

    #region Public Methods and Operators

    /// <summary>
    /// The clone.
    /// </summary>
    /// <returns>
    /// The System.Object.
    /// </returns>
    public object Clone()
    {
      var t = new SchemaRepositoryDsTable();

      foreach (ISchemaItem item in this.MyColumns)
      {
        t.MyColumns.Add(item);
      }

      t.Name = this.Name;
      t.Namespace = this.Namespace;

      return t;
    }

    #endregion
  }
}